#!/bin/bash
# Script to get the bond length and bond energy of H2 from vasp run
# Must be run within: 7-H2-bond-length

if [ -f "../1-H-atom/OUTCAR" ]; then
   EngHAtom=`grep 'free  energy' ../1-H-atom/OUTCAR|awk '{print $5}'`
else
   echo "You need to finish the calculation for H atom in ../1-H-atom first."
   exit 1
fi
if [ -f "OUTCAR" ]; then
   EngH2=`grep 'free  energy' OUTCAR|awk '{print $5}'`
else
   echo "You need to finish the calculation for H2 first."
   exit 1
fi
b_eng=`echo ${EngH2} ${EngHAtom}|awk '{print $1 - 2*$2}'`
#
data=`grep -A3 'POSITION' OUTCAR |tail -2|awk '{print $1}'`
len=`echo $data|awk '{printf "%.3f", sqrt(($2-$1)**2)*sqrt(3.)}'`
#
echo ""
echo "The calculated bond length for H-H is: ${len} Angstrom"
echo "The calculated bond energy for H-H is: ${b_eng} eV"
echo ""

exit 0
